/*
   Copyright 2009 Marcin Szczepanski, webqem (marcins@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package webqem.wave.api
{
	

	public class StubGadgetAPI implements IGadgetAPI
	{
		private var _state:Object = new Object();
		
		private var _mode:String = WaveMode.EDIT;
		
		private var _stateCallback:Function;
		private var _participantCallback:Function;
		private var _modeCallback:Function;
				
		public function StubGadgetAPI()
		{
			trace("Using StubGadgetAPI");
		}
		
		public function get isInWave():Boolean
		{
			return false;
		}
		
		public function initialiseAndSetCallbacks(
            gadgetName:String,
            waveStateCallback:Function, 
            waveParticipantCallback:Function,
            waveModeCallback:Function
            ):void
		{
			_stateCallback = waveStateCallback;
			_participantCallback = waveParticipantCallback;
			_modeCallback = waveModeCallback;
			_stateCallback();
			
		}
		
		public function getWaveMode():String
		{
			return _mode;
		}
		
		public function getState(key:String, defaultValue:String):Object
		{
			if(_state.hasOwnProperty(key)) {
				return _state[key];
			} else {
				return defaultValue;
			}
		}
		
		public function getStateKeys():Array
		{
			var keys:Array = new Array();
			for(var key:String in _state) {
				keys.push(key);
			}
			return keys;
		}
		
		public function submitDelta(delta:Object):void
		{
			for(var key:String in delta) {
				trace("Setting ", key, " to ", delta[key]);
				_state[key] = delta[key];
			}
			_stateCallback();
		}
		
		public function stateToString():String
		{
			var repr:String = "";
			for each(var key:String in _state)
			{
				repr += key + ": " + String(_state[key]) + "\n";		
			}
			return repr;
		}
		
		public function getViewer():String
		{
			return "dummyuser@example.com";
		}
		
		public function getHost():String
		{
			return "dummyuser@example.com";
		}
		
		public function getAllParticipants():Array
		{
			return ["dummyuser@example.com"];
		}
		
		public function getParticipantDisplayName(id:String):String
		{
			return "Stub User";
		}
		
		public function getParticipantThumbnailUrl(id:String):String
		{
			return "";
		}
		
		public function setMode(mode:String):void
		{
			_mode = mode;
	       	_modeCallback(_mode);	
		}
		
	}
}